package com.example.xjtuse_pc.services;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import com.example.xjtuse_pc.mobilesafe.R;
import com.example.xjtuse_pc.receiver.MyWidget;
import com.example.xjtuse_pc.utils.SystemInfoUtils;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class UpdateWidgetService extends Service {
    private ScreenOffReceiver soffr;
    private ScreenOnReceiver sonr;
    private Timer timer;
    private TimerTask task;
    private AppWidgetManager am;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        soffr=new ScreenOffReceiver();
        sonr=new ScreenOnReceiver();
        registerReceiver(soffr,new IntentFilter( Intent.ACTION_SCREEN_OFF));
        registerReceiver(sonr,new IntentFilter( Intent.ACTION_SCREEN_ON));

        am = AppWidgetManager.getInstance(this);
        startTimer();
        super.onCreate();
    }

    private void startTimer() {
        if(timer==null&&task==null){
        timer = new Timer();
        final SystemInfoUtils siu = new SystemInfoUtils(UpdateWidgetService.this);
        task = new TimerTask() {
            @Override
            public void run() {
                Log.i("join", "updata widget");
                //设置更新组件
                ComponentName cn = new ComponentName(UpdateWidgetService.this, MyWidget.class);
                RemoteViews rv = new RemoteViews(getPackageName(), R.layout.process_widget);
                rv.setTextViewText(R.id.process_count, "正在运行的进程:" + siu.getRunningProcessCount() + "个");
                rv.setTextViewText(R.id.process_memory, "可用内存:" + android.text.format.Formatter.formatFileSize(UpdateWidgetService.this, siu.getAvilMem()) + "/" + android.text.format.Formatter.formatFileSize(UpdateWidgetService.this, siu.getLowTotalMem()));
                //定义按钮事件 需要一个自定义广播杀死后台进程
                Intent i = new Intent();
                i.setAction("com.example.xjtuse.killTask");
                PendingIntent pi = PendingIntent.getBroadcast(UpdateWidgetService.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                rv.setOnClickPendingIntent(R.id.btn_clear, pi);
                am.updateAppWidget(cn, rv);

            }
        };
        //开始每三秒执行一次task
        timer.schedule(task, 0, 4000);
    }
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(soffr);
        unregisterReceiver(sonr);
        soffr=null;
        sonr=null;
        TimeDismiss();
        super.onDestroy();
    }

    private void TimeDismiss() {
        if (task != null && timer != null) {
            task.cancel();
            timer.cancel();
            timer = null;
            task = null;
        }
    }
    private class ScreenOnReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("join", "start_screen");
            startTimer();

        }
    }

    private class ScreenOffReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("join", "Lock_screen");
            TimeDismiss();

        }
    }
}
